package com.atguigu.flink0624.chapter11;

import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.DataTypes;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.descriptors.Csv;
import org.apache.flink.table.descriptors.FileSystem;
import org.apache.flink.table.descriptors.Schema;

// 静态导入

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/11/19 10:26
 */
public class Flink04_Sql_BaseUse {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        
        StreamTableEnvironment tenv = StreamTableEnvironment.create(env);
    
        tenv
            .connect(
                new FileSystem().path("input/sensor.txt")
            )
            .withFormat(new Csv())
            .withSchema(new Schema()
                            .field("id", DataTypes.STRING())
                            .field("ts", DataTypes.BIGINT())
                            .field("vc", DataTypes.INT())
            )
            .createTemporaryTable("sensor");
    
        Table table = tenv.from("sensor");
        
        // 1. 查询未注册的表
//        tenv.executeSql(""); // 执行ddl(建表 删表 ...) 和非查询的dml(增删改)
//        tenv.sqlQuery(""); // 执行查询语句
    
//        Table result = tenv.sqlQuery("select * from " + table);
//        result.execute().print();
        // 2. 查询已注册的表
        tenv.createTemporaryView("s1", table);
        
        tenv.sqlQuery("select * from s1").execute().print();
    
    }
}
